<?php
class ShowPage
{
    var $psize=10;
    var $pernum=10;
    var $varstr='';
    var $tpage=0;
    var $pers=0;
    var $recordCount;
    var $isfirst=0;
    var $islast=0;

    /*
     * 当前页码
     * @var integer
     */
    var $page = 0;

    /* 
     * MySQL分页生成语句
     * @var string
     */
    var $limit='';
    var $pagevarname;
    var $offset;
    public function __construct($recordCount,$pageSize,$pagevarname) {
        $this->recordCount = $recordCount;
        $this->psize = $pageSize;
        $this->pagevarname = $pagevarname;
    }

    /*
     * 取得传递变量精数组,并检测它是否为空
     * @return unll
     */
    function get()
    {
        foreach($_GET as $k => $v) {
            $i++;
            $str= ($i==1)? "?": "&";
            $this->varstr = ($k<>"$this->pagevarname") ? $this->varstr.$str.$k."=".urlencode($v) : $this->varstr;
        }
        $this->varstr = $this->varstr ? $this->varstr."&" : "?";
        $this->page = $_GET["$this->pagevarname"] ? $_GET["$this->pagevarname"] : 1;
        if(!is_numeric($this->page))
            $this->page = 1;

        // 用于 MySQL 分页生成语句 //
        $this->offset=($this->page -1)*$this->psize;
        $this->limit= $this->offset.','.$this->psize;
    }

    /*
     * 统计页码数
     */
    function total($number)
    {
        $this->tpage = ceil($number / $this->psize);
        $this->pers= ceil($this->tpage / $this->pernum);
        if($this->page>1)
        {
            $this->isfirst=0; 
        }else{
                $this->isfirst=1;
        }
        if($this->page < $this->tpage)
        {
            $this->islast=0;
        }else 
        {
            $this->islast=1;
        }
        
    }
    /*
     * 分类函数Page(parse page ),$page为当页数
     * $number 为记录总数, $psize 为每页显示数目
     * @return string
     */
    function Page()
    {
    $this->psize= ($psize > 0)? $psize : $this->psize;
	$this->get();
	$this->total($this->recordCount);	
	$setpage = $this->page ? ceil($this->page/$this->pernum) : 1;	
	$pagenum = ($this->tpage > $this->pernum) ? $this->pernum : $this->tpage;
	if ($this->recordCount<= $this->psize) {
            $text= '';
	} else {
          $text = '共'.$this->tpage.'页:&nbsp;&nbsp;';
          //  if ($this->page > 1) {
          //  	$text .= '<li><a title=第一页 href='.$this->varstr.'page=1>1</a></li>';
         //   }	  
            if ($setpage > 1) {
                $lastsetid = ($setpage-1)*$this->pernum;
                $text .= '<a href='.$this->varstr.$this->pagevarname.'='.$lastsetid.'><img src="/images/common/icon_19.gif" /></a>';
            }
            if ($this->page > 1) {
                $pre = $this->page-1;
                $text .= '<a title=上一页 href='.$this->varstr.$this->pagevarname.'='.$pre.'><img src="/images/common/icon_20.gif" /></a>';
               
            }
            $i = ($setpage-1)*$this->pernum;	
            for($j=$i; $j<($i+$pagenum) && $j<$this->tpage; $j++) 
		{
                    $newpage = $j+1;
                    if ($this->page == $j+1) {
                        $text .= ' <a title="本页" onclick="return false"><span class="now">'.($j+1).'</span></a> ';
                    } else {
                        $text .= ' <a href='.$this->varstr.$this->pagevarname.'='.$newpage.'>'.($j+1).'</a> ';
                    }
		}
	
            if ($this->page < $this->tpage){
                $next = $this->page+1;
                $text .= '<a title="下一页" href='.$this->varstr.$this->pagevarname.'='.$next.'><img src="/images/common/icon_21.gif" /></a>';
                
            }	
            if ($setpage < $this->pers) {
                $nextpre = $setpage*($this->pernum)+1;
                $text .= '<a href='.$this->varstr.$this->pagevarname.'='.$nextpre.'><img src="/images/common/icon_22.gif" /></a>';
            }
	
         //   if ($this->page < $this->tpage) {
            ////	$text .= ' ... <a title=最后一页 href='.$this->varstr.'page='.$this->tpage.'>['.$this->tpage.']</a>';
          //  }
	}	
	return $text;
    }
    public function getTotalPage()
    {
        return $this->tpage;
    }
    public function isLast()
    {
        return $this->islast;
    }
    public function isFirst()
    {
        return $this->isfirst;
    }
    public function getCurrentPageNo() {
       return  $this->page;
    }
    function ajaxPage($function_name="showPage")
    {
    $phpSelf=str_replace("modules","ajax",$_SERVER['PHP_SELF']);
    $this->psize= ($psize > 0)? $psize : $this->psize;
	$this->get();
	$this->total($this->recordCount);	
	$setpage = $this->page ? ceil($this->page/$this->pernum) : 1;	
	$pagenum = ($this->tpage > $this->pernum) ? $this->pernum : $this->tpage;
	if ($this->recordCount<= $this->psize) {
            $text= '';
	} else {
          $text = '共'.$this->tpage.'页:&nbsp;&nbsp;';
          //  if ($this->page > 1) {
          //  	$text .= '<li><a title=第一页 href='.$this->varstr.'page=1>1</a></li>';
         //   }	  
            if ($setpage > 1) {
                $lastsetid = ($setpage-1)*$this->pernum;
                $text .= '<a href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$this->pagevarname.'='.$lastsetid.'\')"><img src="/images/common/icon_19.gif" /></a>';
            }
            if ($this->page > 1) {
                $pre = $this->page-1;
                $text .= '<a title=上一页 href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$this->pagevarname.'='.$pre.'\')"><img src="/images/common/icon_20.gif" /></a>';
            }
            $i = ($setpage-1)*$this->pernum;	
            for($j=$i; $j<($i+$pagenum) && $j<$this->tpage; $j++) 
		{
                    $newpage = $j+1;
                    if ($this->page == $j+1) {
                        $text .= ' <a title="本页" onclick="return false"><span class="now">'.($j+1).'</span></a> ';
                    } else {
                        $text .= ' <a href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$this->pagevarname.'='.$newpage.'\')">'.($j+1).'</a> ';
                    }
		}
	
            if ($this->page < $this->tpage){
                $next = $this->page+1;
                $text .= '<a title=下一页 href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$this->pagevarname.'='.$next.'\')"><img src="/images/common/icon_21.gif" /></a>';
            }
	
            if ($setpage < $this->pers) {
                $nextpre = $setpage*($this->pernum)+1;
                $text .= '<a href="javascript:'.$function_name.'(\''.$phpSelf.'\',\''.$this->pagevarname.'='.$nextpre.'\')"><img src="/images/common/icon_22.gif" /></a>';
            }
	
         //   if ($this->page < $this->tpage) {
            ////	$text .= ' ... <a title=最后一页 href='.$this->varstr.'page='.$this->tpage.'>['.$this->tpage.']</a>';
          //  }
	}	
	return $text;
    }  
}

?>
